<template>
	<view class="page">
		<view class="header">
			<view class="header-title">
				<image src="/static/images/rank/trophy.png"></image>
				<text>绩效考核</text>
			</view>
			<text class="header-date">-- 2024.12 --</text>
		</view>
		<view class="ranking">
			<scroll-view class="scroll-view_x" scroll-x="true" scroll-left="0">
				<view class="rangking-title">
					<text>排名</text>
					<text>业务人员</text>
					<text>当前贷款余额(万元)</text>
					<text>较上月末贷款余额(万元)</text>
					<text>较去年末贷款余额(万元)</text>
					<text>客户数</text>
					<text>较上月末客户数</text>
					<text>较去年末客户数</text>
				</view>
				<scroll-view scroll-y="true" class="scroll-view_y" :scroll-top="scrollTop"  @scroll="onScroll" @scrolltolower="loadMore" @refresherrefresh="refreshRank">
					<view class="ranking-list" :style="{ height: scrollHeight + 'px' }">
						<view class="ranking-list-item" v-for="(item, key) in list" :key="key">
							<view class="ranking-list-index">	
								<span v-if="item.ranking <= 3" class="ranking-list-number">
									<image :src="'/static/images/rank/' + (item.ranking) + '.png'"></image>
								</span>
								<span v-else class="ranking-list-number">
									<text>{{item.ranking}}</text>
								</span>
								<text @click="show(item.ehrName)">{{item.ehrName}}</text>
								<text>{{item.currLoadBal}}</text>
								<text :class="['number', { positive: item.lmLoadBal > 0, negative: item.lmLoadBal < 0 }, { up: item.lmLoadBal > 0, down: item.lmLoadBal < 0 }]">{{item.lmLoadBal}}</text>
								<text>{{item.lyLoadBal}}</text>
								<text>{{item.currCustNum}}</text>
								<text>{{item.lmCustNum}}</text>
								<text>{{item.lyCustNum}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</scroll-view>
		</view>
		<!-- 返回顶部图标 -->
		<view  v-if="showBackToTop" class="back-to-top" @click="scrollToTop">
		  <image src="/static/images/rank/backTop.jpeg" mode="aspectFit"></image>
		</view>
	</view>
</template>

<script>
	import { listPerformanceRankingPC } from "@/api/boc/performanceRankingPC"
	export default {
		data(){
			return {
				// 初始化滚动高度
				scrollHeight: 0,
				list: [],
				total:0,
				queryParams: {
				    pageNum: 1,
				    pageSize: 20
				  },
				//返回顶部
				scrollTop: 0,
			    showBackToTop: false,  				  
			}
		},
		onLoad() {
			// 页面加载时设置scroll-view的高度
			this.setScrollViewHeight();
			this.getPerformanceRankingPC();
		},
		methods: {
		  show(e) {
		    alert(e);
		  },
		  setScrollViewHeight() {
			// 获取系统信息，包括屏幕高度
			uni.getSystemInfo({
			  success: (info) => {
				// 假设顶部有个固定高度的header（例如：100px），底部有个固定高度的footer（例如：50px）
				const headerHeight = 100;
				const footerHeight = 50;
				// 计算scroll-view的可滚动高度
				this.scrollHeight = info.windowHeight - headerHeight - footerHeight-70;
			  }
			});
		  },
		  loadMore(){
			  let maxPage = (this.total/this.queryParams.pageSize) | 0;
			  if(this.queryParams.pageNum <= maxPage){
				  this.queryParams.pageNum = this.queryParams.pageNum+1;
				  this.getPerformanceRankingPC();
			  }  
		  },
		  refreshRank(){
			  alert("下拉刷新,最新排名");
			  //this.$refs.scrollView.triggerRefresh()
		  },
		  getPerformanceRankingPC() {
		    listPerformanceRankingPC(this.queryParams).then(response => {
			  this.total = response.total;
		      this.list =this.list.concat(response.rows);
		    })
		  },
		  onScroll(event) {
			this.scrollTop = event.detail.scrollTop;
			this.showBackToTop = this.scrollTop > 300; // 当滚动超过300px时显示图标
		  },
		  scrollToTop() {
			// 解决view层不同步的问题
			this.scrollTop = this.scrollTop
			this.$nextTick(function() {
				this.scrollTop = 0
			});
			this.showBackToTop = false; // 滚动到顶部后隐藏图标
		  },
		}
	}
</script>

<style lang="scss">
	page{
		background: #b6002a;
		height: 100%;
	}
	
	.page{
		padding-bottom: 20rpx;
		
		.header{
			display: flex;
			flex-direction: column;
			align-items: center;
			color: #fff;
			padding: 20rpx;
			
			.header-title{
				display: block;
				background: #F2856E;
				width: 400rpx;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 20rpx;
				font-size: 18px;
				text-align: center;
				font-weight: bold;
				display: flex;
				align-items: center;
				justify-content: center;
				
				image{
					width: 50rpx;
					height: 50rpx;
					margin-right: 8rpx;
				}
			}
			
			.header-date{
				margin: 20rpx;
				font-weight: bold;
			}
		}
		
		.ranking{
			width: 700rpx;
			border-radius: 30rpx;
			margin: auto;
			background: #fff;
			box-sizing: border-box;
			padding: 20rpx;
			
			.scroll-view_y{
				width: 1070px;
				flex: 1;
			}
			
			.rangking-title{
				display: flex;
				font-size: 14px;
				color: #555;
				width: 1100px;
				height: 50px;
				line-height: 50px;		
				border-bottom: 2px solid #f0f0f0;
				text{
					display: block;
					width: 100px;
					text-align: center;					
					&:nth-child(2){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 100px
					}
					&:nth-child(3){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 160px
					}
					&:nth-child(4){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 180px
					}
					&:nth-child(5){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 180px
					}
					&:nth-child(6){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 100px
					}
					&:nth-child(7){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 140px
					}
					&:nth-child(8){
						box-sizing: border-box;
						padding-left: 20px;
						text-align: left;
						width: 140px
					}
				}
			}
			.ranking-list{
				.ranking-list-item{
					display: block;
					align-items: center;
					font-size: 14px;
					.ranking-list-number{
						width: 100px;
						color: #777;
						display: flex;
						justify-content: center;
						
						image{
							width: 50rpx;
							height: 50rpx;
						}
					}
					.ranking-list-index{
						display: flex;
						font-size: 14px;
						color: #555;
						width: 1070px;
						height: 50px;
						line-height: 50px;	
						text-align: center;
						align-items: center;
						justify-content: center;
						border-bottom: 1px solid #f0f0f0;
						text{
							display: block;
							text-align: center;					
							&:nth-child(2){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 100px
							}
							&:nth-child(3){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 160px
							}
							&:nth-child(4){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 180px
							}
							&:nth-child(5){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 180px
							}
							&:nth-child(6){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 100px
							}
							&:nth-child(7){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 140px
							}
							&:nth-child(8){
								box-sizing: border-box;
								padding-left: 20px;
								text-align: center;
								width: 140px
							}
						}
						.number {
						  margin: 0 10px;
						  font-size: 16px;
						}
						 
						.positive {
						  color: #FF0000; /* 红色 */
						}
						 
						.negative {
						  color: #008000; /* 绿色 */
						}
						 
						.arrow {
						  font-size: 20px;
						}
						 
						.up::after {
						  content: '↑';
						}
						 
						.down::after {
						  content: '↓';
						}
					}
				}
			}
			
		}
		.back-to-top {
		  position: fixed;
		  right: 20px;
		  bottom: 60px;
		  width: 40px;
		  height: 40px;
		  background-color: rgba(0, 0, 0, 0.5);
		  border-radius: 50%;
		  display: flex;
		  align-items: center;
		  justify-content: center;
		  cursor: pointer;
		}
		 
		.back-to-top image {
		  width: 40px;
		  height: 40px;
		}
	}
</style>